import configTool from '../../com/configTool';
import LogTool from '../../com/LogTool/LogTool';




function logHandler() {
    return async (ctx: any, next: Function) => {
        await configTool.init();

        const logObj = {
            ip: ctx.request.ip,
            method: ctx.request.method,
            start: new Date().toLocaleString(),
            passSecond: 0,
            url: decodeURI(ctx.request.url)
        };
        await next();

        if (logObj.url === '/'
            || logObj.url.startsWith('/MapResources/')
            || logObj.url.startsWith('/Resources/')
            || logObj.url.startsWith('/Web')
            || logObj.url.startsWith('/favicon.ico')) {
            return;
        }

        logObj.passSecond = (new Date().getTime() - new Date(logObj.start).getTime()) / 1000;
        const logger = await LogTool.getLogger();
        logger.info(`http`, {message: JSON.stringify(logObj)});
    };
}


export default logHandler;
